#include "ihm.h"


IHM::IHM(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	connect(ui.actionPitchDetector, SIGNAL(triggered()), this, SLOT(launchPitchDetector()));
	aManager=new Manager(12);
	QDesktopWidget screen;
	ui.tracksScrollArea->adjustSize();

	QRect screenGeom = screen.screenGeometry(this);
	populateIHM();
	int screenCenterX = screenGeom.center().x();
	int screenCenterY = screenGeom.center().y();
  
	move(screenCenterX - width () / 2,screenCenterY - height() / 2);
	
	this->showMaximized();

	currentTrack=0;
	qvisual=new QVisualizer();
	qchorus=new QChorus(aManager,&currentTrack);
	qdist=new QDistortion(aManager,&currentTrack);
	//ui.verticalLayout->addWidget(qvisual);
	ui.verticalLayout->addWidget(qchorus);
	ui.verticalLayout->addWidget(qdist);
	for(int i=0;i<9;i++){
		ras[i]=NULL;
	}
	metronome=NULL;
	//detector=new QPitchDetector(aManager->system);
	//ui.horizontalLayout_6->addWidget(detector);
}

IHM::~IHM()
{

}

void IHM::selectFile()
{
	path = QFileDialog::getOpenFileName (this, tr("Directory"), directory.path());
	if ( path.isNull() == false )
	{
		directory.setPath(path);
		ui.statusBar->showMessage(path, 0);
		QByteArray ba = path.toLocal8Bit();
		char *c_str2 = ba.data();
		aManager->addClip(c_str2);
		vector<char *> filesName= aManager->getClipsInfos();
		ui.optionsListWidget->clear();
		for(int i=0;i<filesName.size();i++)
		{
			QString s = QString(filesName[i]);
			ui.optionsListWidget->addItem(s);
		}
	}
}

void IHM::launchMetronome(){
		if(metronome!=NULL)
			metronome->terminate();
		metronome=NULL;
		metronome=new QMetronome(aManager->system,ui.tempoSpinBox->value(),ui.tapePerMesure->value(),ui.duration->value());
		metronome->start();
}

void IHM::stopAll(){
	if(metronome!=NULL){
		metronome->getChannel()->stop();
		metronome->terminate();
		metronome=NULL;
	}
	for(int i=0;i<9;i++){
		if(ras[i]!=NULL)
			ras[i]->terminate();
		aManager->stopAll();
		tracks[i].vuMeter->setLeftValue(-60);
		tracks[i].vuMeter->setRightValue(-60);
	}
}

void IHM::play()
{
	stopAll();
	int index = ui.optionsListWidget->currentRow();
	if(index!=-1){
			if(ui.onOffMetronome->isChecked()){
				launchMetronome();
			}
		
		int track=0;
		QPushButton * b = qobject_cast<QPushButton *>(sender());
		if (b) {
			for(int i=0;i<9;i++){
				if(i!=0){
					for(int j=0;j<8;j++){
						if(b==sampleButton[(i*8)+j]){
							if(aManager->getNbClips()==0)
								return;
							b->setText(aManager->getClipsInfos().at(index));
							aManager->setClip(index,j,track);
							aManager->play(j,track);
							if(!aManager->isPlaying(track,j)){
								if(ras[track]!=NULL)
									ras[track]->terminate();
							}
							else{
								if(aManager->isAssign(track,j)){
									ras[track]=new RealTimeAnalysis(track,tracks[track].vuMeter,aManager,0,qvisual);
								}
							}
							tracks[track].vuMeter->setLeftValue(-60);
							tracks[track].vuMeter->setRightValue(-60);
						}
					}
				}
				else{
					for(int j=0;j<8;j++){
						if(b==sampleButton[(i*8)+j]){	
							if(!aManager->isPlaying(track,j)){
									if(ras[track]!=NULL){
										ras[track]->terminate();
								}
							}
							for(int k=1;k<8;k++){
								if(aManager->isAssign(k,j)){
									play(k, j);
								}
							}
						}
					}
				}
				track++;
			}
    
		}
		ras[0]=new RealTimeAnalysis(0,tracks[0].vuMeter,aManager,0,qvisual);
	}
}

void IHM::play(int track, int row){
		aManager->play(row,track);
		if(ras[track]!=NULL)
			ras[track]->terminate();
		ras[track]=new RealTimeAnalysis(track,tracks[track].vuMeter,aManager,0,qvisual);
		tracks[track].vuMeter->setLeftValue(-60);
		tracks[track].vuMeter->setRightValue(-60);
}

void IHM::setPan(int value){
	QDial * b = qobject_cast<QDial *>(sender());
    if (b) {
			for(int i=0;i<9;i++){
				if(b==tracks[i].panDial)
					setPan(i,value);
			}
    }
}
void IHM::setVolume(int volume){
	QSlider * b = qobject_cast<QSlider *>(sender());
    if (b) {
			for(int i=0;i<9;i++){
				if(b==tracks[i].volumeSlider)
					setVolume(i,volume);
			}
    }
}

void IHM::setMute(){
	QPushButton * b = qobject_cast<QPushButton *>(sender());
    if (b) {
			for(int i=0;i<9;i++){
				if(b==tracks[i].muteButton)
					aManager->setMuteOnTrack(i);
			}
    }
}
void IHM::setSolo(){
	QPushButton * b = qobject_cast<QPushButton *>(sender());
    if (b) {
			for(int i=0;i<9;i++){
				if(b==tracks[i].soloButton)
					aManager->setSoloOnTrack(i);
			}
    }
}

void IHM::setPan(int trackID,int value){
	float v= ((float)value)/100;
	aManager->setPanTrack(trackID,v);
}
void IHM::setVolume(int trackID,int volume){
	float v= ((float)volume)/100;
	aManager->setGainTrack(trackID,v);
}

void IHM::setRec(){
	QPushButton * b = qobject_cast<QPushButton *>(sender());
    if (b) {
			for(int i=0;i<9;i++){
				if(b==tracks[i].recButton){
					aManager->setRecOnTrack(i);
					b->setText("REC ON");
					currentTrack=i;
					updatePluginsValues();
				}
				else{
					tracks[i].recButton->setText("REC OFF");
				}
			}
    }
}

void IHM::populateIHM(){

	
	tracks[0].muteButton=ui.master_muteButton;
	tracks[1].muteButton=ui.t1_muteButton;
	tracks[2].muteButton=ui.t2_muteButton;
	tracks[3].muteButton=ui.t3_muteButton;
	tracks[4].muteButton=ui.t4_muteButton;
	tracks[5].muteButton=ui.t5_muteButton;
	tracks[6].muteButton=ui.t6_muteButton;
	tracks[7].muteButton=ui.t7_muteButton;
	tracks[8].muteButton=ui.t8_muteButton;
	
	tracks[0].panDial=ui.master_panDial;
	tracks[1].panDial=ui.t1_panDial;
	tracks[2].panDial=ui.t2_panDial;
	tracks[3].panDial=ui.t3_panDial;
	tracks[4].panDial=ui.t4_panDial;
	tracks[5].panDial=ui.t5_panDial;
	tracks[6].panDial=ui.t6_panDial;
	tracks[7].panDial=ui.t7_panDial;
	tracks[8].panDial=ui.t8_panDial;
	
	tracks[0].recButton=ui.master_recButton;
	tracks[1].recButton=ui.t1_recButton;
	tracks[2].recButton=ui.t2_recButton;
	tracks[3].recButton=ui.t3_recButton;
	tracks[4].recButton=ui.t4_recButton;
	tracks[5].recButton=ui.t5_recButton;
	tracks[6].recButton=ui.t6_recButton;
	tracks[7].recButton=ui.t7_recButton;
	tracks[8].recButton=ui.t8_recButton;
	
	tracks[0].soloButton=ui.master_soloButton;
	tracks[1].soloButton=ui.t1_soloButton;
	tracks[2].soloButton=ui.t2_soloButton;
	tracks[3].soloButton=ui.t3_soloButton;
	tracks[4].soloButton=ui.t4_soloButton;
	tracks[5].soloButton=ui.t5_soloButton;
	tracks[6].soloButton=ui.t6_soloButton;
	tracks[7].soloButton=ui.t7_soloButton;
	tracks[8].soloButton=ui.t8_soloButton;
	
	tracks[0].volumeSlider=ui.master_volumeSlider;
	tracks[1].volumeSlider=ui.t1_volumeSlider;
	tracks[2].volumeSlider=ui.t2_volumeSlider;
	tracks[3].volumeSlider=ui.t3_volumeSlider;
	tracks[4].volumeSlider=ui.t4_volumeSlider;
	tracks[5].volumeSlider=ui.t5_volumeSlider;
	tracks[6].volumeSlider=ui.t6_volumeSlider;
	tracks[7].volumeSlider=ui.t7_volumeSlider;
	tracks[8].volumeSlider=ui.t8_volumeSlider;
	
	tracks[0].vuMeter=ui.master_qvumeter;
	tracks[1].vuMeter=ui.t1_qvumeter;
	tracks[2].vuMeter=ui.t2_qvumeter;
	tracks[3].vuMeter=ui.t3_qvumeter;
	tracks[4].vuMeter=ui.t4_qvumeter;
	tracks[5].vuMeter=ui.t5_qvumeter;
	tracks[6].vuMeter=ui.t6_qvumeter;
	tracks[7].vuMeter=ui.t7_qvumeter;
	tracks[8].vuMeter=ui.t8_qvumeter;
	
	tracks[0].sampleMatrice=ui.matriceButton_master;
	tracks[1].sampleMatrice=ui.matriceButton_t_1;
	tracks[2].sampleMatrice=ui.matriceButton_t_2;
	tracks[3].sampleMatrice=ui.matriceButton_t_3;
	tracks[4].sampleMatrice=ui.matriceButton_t_4;
	tracks[5].sampleMatrice=ui.matriceButton_t_5;
	tracks[6].sampleMatrice=ui.matriceButton_t_6;
	tracks[7].sampleMatrice=ui.matriceButton_t_7;
	tracks[8].sampleMatrice=ui.matriceButton_t_8;
	QIcon *icon =new QIcon(":/IHM/ressources/stop.png");
	for(int i=0;i<9;i++){
		for(int j=0;j<8;j++){
			QPushButton *b=new QPushButton();

			b->setText("Play");
			b->setIcon(*icon);

			sampleButton[(i*8)+j]=b;

			tracks[i].sampleMatrice->layout()->addWidget(b);
			b->connect(b,SIGNAL(clicked()),this,SLOT(play()));
		}
	}
}

void IHM::comp_setAtck(int value){
	aManager->comp_setAtck(currentTrack,value);
}
void IHM::comp_setRelease(int value){
	aManager->comp_setRelease(currentTrack,value);
}
void IHM::comp_setThreshold(int value){
	aManager->comp_setThreshold(currentTrack,value);
}
void IHM::comp_setGain(int value){
	aManager->comp_setGain(currentTrack,value);
}
void IHM::comp_activate(){
	aManager->comp_setActive(currentTrack);
}

void IHM::updatePluginsValues(){
	float values[8];
	aManager->getPluginsValues(currentTrack,values);
	//FX Compressor update
	ui.comp_atckSlider->setValue(values[0]);
	ui.comp_gainSlider->setValue(values[1]);
	ui.comp_releaseSlider->setValue(values[2]);
	ui.comp_thresholdSlider->setValue(values[3]);
	
}

void IHM::setBPM(int bpm){
	if(metronome!=NULL)
		metronome->setTempo(bpm);
}

void IHM::setTPM(int mesure){
	if(metronome!=NULL)
		metronome->setMesure1(mesure);

}
void IHM::setDuration(int duration){
	if(metronome!=NULL)
		metronome->setMesure2(duration);
}

void IHM::launchPitchDetector(){
	QDialog dial;
	
	QPitchDetector pdec;
	pdec.show();
}